package com.bkgy.modular.subjectQuery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bkgy.modular.subjectQuery.entity.KeyValueEntity;
import com.bkgy.modular.subjectQuery.entity.ShareData;
import com.bkgy.modular.subjectQuery.entity.TbDsjC621EdgePassTime;
import com.bkgy.modular.subjectQuery.entity.TbDsjPdiC621;
import com.bkgy.modular.subjectQuery.mapper.TbDsjC621EdgePassTimeMapper;
import com.bkgy.modular.subjectQuery.mapper.TbDsjPdiC621Mapper;
import com.bkgy.modular.subjectQuery.service.RightDataTabService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName coldRollingPDIServiceImpl
 * @date 2023/1/8 16:39
 */
@Service("cold_pdi")
public class ColdRollingPDIServiceImpl implements RightDataTabService {
    @Resource
    private TbDsjC621EdgePassTimeMapper tbDsjC621EdgePassTimeMapper;

    @Resource
    private TbDsjPdiC621Mapper tbDsjPdiC621Mapper;

    @Override
    public ResultData findByRightData(Integer pageIndex, Integer pageSize, String coilId, String HotId, ShareData shareData) {
        // 根据coilId去TB_DSJ_C621_EDGE_PASS_TIME表中查询出HOT_COIL_ID
        // 创建查询条件构造器
        LambdaQueryWrapper<TbDsjC621EdgePassTime> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TbDsjC621EdgePassTime::getCoil_id,coilId);
        TbDsjC621EdgePassTime tbDsjC621EdgePassTime = tbDsjC621EdgePassTimeMapper.selectOne(queryWrapper);

        // 再次创建条件构造器
        LambdaQueryWrapper<TbDsjPdiC621> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(TbDsjPdiC621::getCust_mat_no,tbDsjC621EdgePassTime.getHot_coil_id());

        List<TbDsjPdiC621> tbDsjPdiC621List = tbDsjPdiC621Mapper.selectList(queryWrapper2);
        List<KeyValueEntity> list = new ArrayList<>();
        for (TbDsjPdiC621 tbDsjPdiC621 : tbDsjPdiC621List) {
            Class c621Class = tbDsjPdiC621.getClass();
            Field[] declaredFields = c621Class.getDeclaredFields();
            for (int i = 0; i < declaredFields.length; i++) {
                Field field = declaredFields[i];
                field.setAccessible(true);
                KeyValueEntity keyValueEntity = new KeyValueEntity();
                keyValueEntity.setKey(field.getName());
                try {
                    keyValueEntity.setValue(field.get(tbDsjPdiC621));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                list.add(keyValueEntity);
            }
        }
        List<KeyValueEntity> entityList = list.stream().skip((pageIndex - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        return ResultData.ResultDataSuccess(entityList, list.size());
    }
}
